package com.ruoyi.mp.controller;

import com.egzosn.pay.common.util.sign.encrypt.MD5;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.db.domain.system.MpClient;
import com.ruoyi.db.domain.system.MpClientRaiseRecord;
import com.ruoyi.db.domain.web.MpClientSubCart;
import com.ruoyi.mp.annotion.Login;
import com.ruoyi.mp.annotion.LoginUser;
import com.ruoyi.mp.service.IMpClientRaiseRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * 用户参与众筹活动记录Controller
 * 
 * @author ruoyi
 * @date 2022-08-03
 */
@Api(value = "用户参与众筹活动记录",tags ="用户参与众筹活动记录")
@RestController
@RequestMapping("/smp/raise/record")
public class MpClientRaiseRecordController extends BaseController
{
    private static Logger log = LoggerFactory.getLogger(MpClientRaiseRecordController.class);
    @Autowired
    private IMpClientRaiseRecordService mpClientRaiseRecordService;
    public final static String key = "T6hCxKDrG5E9DhIccGvJ6VXycJs09gb2";


    /**
     * 查询用户参与众筹活动记录列表
     */
    @Login
    @ApiOperation(value = "查询用户参与众筹活动记录列表")
    @GetMapping("/list")
    public TableDataInfo list(Integer takeType,@LoginUser MpClient client)
    {
        try {
            startPage();
            MpClientRaiseRecord raiseRecord = new MpClientRaiseRecord();
            raiseRecord.setTakeType(takeType);
            raiseRecord.setClientId(client.getClientId());
            List<MpClientRaiseRecord> list = mpClientRaiseRecordService.selectMpClientRaiseRecordList(raiseRecord);
            return getDataTable(list);
        }catch (Exception e){
            log.error("用户参与众筹活动记录发生异常",e);
        }
        return null;
    }


    /**
     * 获取用户参与众筹活动记录详细信息
     */
    @ApiOperation(value = "获取用户参与众筹活动记录详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        try {
            return AjaxResult.success(mpClientRaiseRecordService.selectMpClientRaiseRecordById(id));
        }catch (Exception e){
            log.error("用户参与众筹活动记录发生异常",e);
        }
        return AjaxResult.error("用户参与众筹活动记录异常");
    }

    /**
     *预购
     */
    @Login
    @ApiOperation(value = "预购")
    @PostMapping("/order")
    public AjaxResult order(@RequestBody MpClientRaiseRecord mpClientRaiseRecord, @LoginUser MpClient client,@RequestParam String sign)
    {
        String text= "amount"+mpClientRaiseRecord.getOrderAmount()+"_"+"raiseId"+mpClientRaiseRecord.getRaiseId();
        String encrySign= MD5.sign(text,key,"utf-8");
        if(!encrySign.equals(sign)){
            return AjaxResult.error("签名验证失败");
        }
        try {
            mpClientRaiseRecord.setClientId(client.getClientId());
            mpClientRaiseRecordService.orderRecord(mpClientRaiseRecord,client);
            return AjaxResult.success("预购成功");
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     *现购
     */
    @Login
    @ApiOperation(value = "现购")
    @PostMapping("/now")
    public AjaxResult add(@RequestBody MpClientRaiseRecord mpClientRaiseRecord, @LoginUser MpClient client,@RequestParam String sign)
    {
        String text= "amount"+mpClientRaiseRecord.getNowAmount()+"_"+"raiseId"+mpClientRaiseRecord.getRaiseId();
        String encrySign= MD5.sign(text,key,"utf-8");
        if(!encrySign.equals(sign)){
            return AjaxResult.error("签名验证失败");
        }
        try {
            mpClientRaiseRecord.setClientId(client.getClientId());
            mpClientRaiseRecordService.nowRecord(mpClientRaiseRecord,client);
            return AjaxResult.success("抢打成功");
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }
    /**
     * 使用道具卡
     */
    @Login
    @ApiOperation(value = "使用道具卡")
    @PostMapping("/use/card")
    public AjaxResult useCard(@RequestBody MpClientRaiseRecord mpClientRaiseRecord, @LoginUser MpClient client)
    {
        try {
            mpClientRaiseRecord.setClientId(client.getClientId());
            mpClientRaiseRecordService.useCard(mpClientRaiseRecord,client);
            return AjaxResult.success("使用成功");
        }catch (Exception e){
            log.error("使用道具卡发生异常",e);
        }
        return AjaxResult.error("使用道具卡异常");
    }
    /**
     * 使用道具卡
     */
    @ApiOperation(value = "获取优先预约需要消耗的银藻")
    @GetMapping("/getFirstOrder/silver")
    public AjaxResult getFirstSilver(BigDecimal totalAmount)
    {
        try {
            BigDecimal rate = mpClientRaiseRecordService.getFirstSilver(totalAmount);
            return AjaxResult.success(rate);
        }catch (Exception e){
            log.error("使用道具卡发生异常",e);
        }
        return AjaxResult.error("使用道具卡异常");
    }
    /**
     * 判断用户是否已经参与游戏,是否已经使用道具卡
     */
    @Login
    @ApiOperation(value = "判断用户是否已经参与游戏,是否已经使用道具卡")
    @GetMapping("/isTakeRaise")
    public AjaxResult isTakeRaise(MpClientRaiseRecord raiseRecord, @LoginUser MpClient client)
    {
        try {
            raiseRecord.setClientId(client.getClientId());
            Integer useStatus = mpClientRaiseRecordService.isTakeRaise(raiseRecord);
            return AjaxResult.success(useStatus);
        }catch (Exception e){
            log.error("使用道具卡发生异常",e);
        }
        return AjaxResult.error("使用道具卡异常");
    }


}
